﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql;
using FreeSql.Internal.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class TestController : BaseController
    {
        private readonly ILogger<TestController> _logger;
        public TestController(ILogger<TestController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index([FromForm] DateTime begintime, [FromForm] DateTime endtime, [FromForm] int doPage = 1, [FromForm] int pageNumber = 1, [FromForm] int pageSize = 10)
        {
            // 建表
            await _fsql.Select<LuXian>().ToListAsync();

            // 生成数据
            int ryIndex = new Random().Next(0, 3); // 人员索引
            string[] ry = { "张三", "李四", "王五"};

            int stIndex = new Random().Next(0, 3); // 状态索引
            string[] st = { "正常", "异常", "未检" };
            List<RenWu> renwus = await _fsql.Select<RenWu>().Where(rw => rw.Rq == DateTime.Parse(DateTime.Now.ToShortDateString())).ToListAsync();
            List<LuXian> luxians = await _fsql.Select<LuXian>().ToListAsync();

            List<XunJianShuJu> xunjianshujus = new();
            foreach (var renwu in renwus)
            {
                List<Dictionary<string, string>> daan = new();
                Dictionary<string, string> dq01 = new();
                Dictionary<string, string> dq02 = new();
                Dictionary<string, string> qj01 = new();
                Dictionary<string, string> qj02 = new();
                Dictionary<string, string> gl01 = new();
                Dictionary<string, string> gl02 = new();
                foreach (var luxian in luxians)
                {
                    XunJianShuJu xunjianshuju = new() { Rwid = renwu.Id, Sbid = luxian.Id, Xjsj = DateTime.Now, Duty = renwu.Duty, Shift = renwu.Shift, Inspector = ry[ryIndex], Xjlx = luxian.Lxmc, Xjsb = luxian.Xjx, Xjbz = luxian.Content, Status = st[stIndex], Result = $"工作中{luxian.Id}" };
                    xunjianshujus.Add(xunjianshuju);
                    // 写6条路线的答案
                    if (luxian.Lxmc.Equals("电气01")) {
                        dq01.Add(luxian.Dd, st[stIndex]);
                        dq01.Add(luxian.Ee, $"工作中{luxian.Id}");
                    }
                    if (luxian.Lxmc.Equals("电气02"))
                    {
                        dq02.Add(luxian.Dd, st[stIndex]);
                        dq02.Add(luxian.Ee, $"工作中{luxian.Id}");
                    }
                    if (luxian.Lxmc.Equals("汽机01"))
                    {
                        qj01.Add(luxian.Dd, st[stIndex]);
                        qj01.Add(luxian.Ee, $"工作中{luxian.Id}");
                    }
                    if (luxian.Lxmc.Equals("汽机02"))
                    {
                        qj02.Add(luxian.Dd, st[stIndex]);
                        qj02.Add(luxian.Ee, $"工作中{luxian.Id}");
                    }
                    if (luxian.Lxmc.Equals("锅炉01"))
                    {
                        gl01.Add(luxian.Dd, st[stIndex]);
                        gl01.Add(luxian.Ee, $"工作中{luxian.Id}");
                    }
                    if (luxian.Lxmc.Equals("锅炉02"))
                    {
                        gl02.Add(luxian.Dd, st[stIndex]);
                        gl02.Add(luxian.Ee, $"工作中{luxian.Id}");
                    }
                }
                //daan.Add("dq01", dq01);
                //daan.Add("dq02", dq02);
                //daan.Add("qj01", qj01);
                //daan.Add("qj02", qj02);
                //daan.Add("gl01", gl01);
                //daan.Add("gl02", gl02);
                daan.Add(dq01);
                daan.Add(dq02);
                daan.Add(qj01);
                daan.Add(qj02);
                daan.Add(gl01);
                daan.Add(gl02);
                await _fsql.Update<RenWu>(renwu.Id).Set(rw => rw.Daan, JsonConvert.SerializeObject(daan)).Set(rw => rw.Status, "已关闭").ExecuteAffrowsAsync();
            }
            await _fsql.Insert(xunjianshujus).ExecuteAffrowsAsync();
            JObject data = new();
            data.Add("xunjianshujus", JArray.FromObject(xunjianshujus));
            return RenderJson(data: data);
        }
    }
}
